/*============================================================================
 PROJECT: Halalgems
 FILE:    LocationService.h
 AUTHOR:  Nguyen Minh Khoai
 =============================================================================*/

/*============================================================================
 IMPORT
 =============================================================================*/
#import "LocationService.h"
#import "Utils.h"

/*============================================================================
 MACRO
 =============================================================================*/

/*============================================================================
 PRIVATE INTERFACE
 =============================================================================*/
#define kLatestLocationKey              @"kLatestLocationKey"

/*----------------------------------------------------------------------------
 Interface:   LocationService
 -----------------------------------------------------------------------------*/
@interface LocationService()
@property (nonatomic, strong)   CLLocationManager *locationManager;
@end

@implementation LocationService

/*----------------------------------------------------------------------------
 Method:      init
 init local variable
 -----------------------------------------------------------------------------*/
- (id)init {
    self = [super init];
    if (self) {
    }
    return  self;
}

- (void)callBlockWith:(CLLocation *)location error:(NSError *)error {
    if(_updateLocationCallback) {
        _updateLocationCallback(location, error);
    }
}

/*----------------------------------------------------------------------------
 Method:      getUpdateLocationWithSuccessBlock:errorBlock:
 get current location of user
 -----------------------------------------------------------------------------*/
- (void)getUpdateLocationWithCallback:(void (^)(CLLocation *location, NSError *error))callback {
    
    /* keep blocks to use later */
    self.updateLocationCallback = callback;
    
    /* check internet first, if has no connection, don't try to get new location */
    if(!appDelegate.hasNetwork) {
        
        /* get cache latest */
        NSMutableArray *locations = [FileHelper restoreArrayForKey:kLatestLocationKey];
        [self callBlockWith:[locations lastObject] error:MKError(kNoConnectionErrorCode, @"")];
        
        return;
    }
    
    /* If location service is enable, try to get device's current location.
     Otherwise, force show alert to indicate location service is disable and settings option*/
    if(![CLLocationManager locationServicesEnabled]
       || [CLLocationManager authorizationStatus] == kCLAuthorizationStatusDenied) {
        
        NSString *domain = [[NSBundle mainBundle] objectForInfoDictionaryKey:@"CFBundleDisplayName"];
        NSMutableDictionary *userInfo = [NSMutableDictionary dictionary];
        
        NSString *errorMsg = NSLocalizedString(@"Location service is disabled. Go Setting and enable it to use our service", nil);
        userInfo[@"ErrorMessage"] = errorMsg;
        
        NSError *error = [NSError errorWithDomain:domain code:501 userInfo:userInfo];
        [self callBlockWith:nil error:error];
    }
    
    if(!_locationManager) {
        self.locationManager = [[CLLocationManager alloc] init];
        [_locationManager setDesiredAccuracy:kCLLocationAccuracyBest];
    }
    _locationManager.delegate = self;
    [_locationManager startMonitoringSignificantLocationChanges];
    [_locationManager startUpdatingLocation];
}

#pragma mark - implement CLLocationManagerDelegate

- (void)locationManager:(CLLocationManager *)manager
	didUpdateToLocation:(CLLocation *)newLocation
           fromLocation:(CLLocation *)oldLocation {
    
    /* save cache latest */
    [FileHelper backupArray:@[newLocation] forKey:kLatestLocationKey];
    
    /* call block */
    [self callBlockWith:newLocation error:nil];
    
    /* stop update location */
    if(!_tracking) {
        _locationManager.delegate = nil;
        
        [_locationManager stopMonitoringSignificantLocationChanges];
        [_locationManager stopUpdatingLocation];
    }
    
    [self stopUpdatingLocation];
}

- (void)locationManager:(CLLocationManager *)manager
       didFailWithError:(NSError *)error {
    
    /* get cache latest */
    NSMutableArray *locations = [FileHelper restoreArrayForKey:kLatestLocationKey];
    [self callBlockWith:[locations lastObject] error:error];
    
    /* stop update location */
    if(!_tracking) {
        _locationManager.delegate = nil;
        [_locationManager stopUpdatingLocation];
    }
    [self stopUpdatingLocation];
}

- (void)stopUpdatingLocation {
    [_locationManager stopMonitoringSignificantLocationChanges];
    [_locationManager stopUpdatingLocation];
    _locationManager.delegate = nil;
}
@end
